var addresshead = document.querySelector(".addresshead");
var close = document.querySelector(".addressbody .close");
var addressbody = document.querySelector(".addressbody");
var lis = document.querySelectorAll(".city-bar-head ul li");
var citybody = document.querySelectorAll(".city-bar-body ul");
var cityhead = document.querySelectorAll(".city-bar-head ul li");

addresshead.onclick = function(el) {
  addresshead.classList.add("addressheadchange");
  addressbody.style.display = "block";
  el.stopPropagation();
};
close.onclick = function() {
  addresshead.classList.remove("addressheadchange");
  addressbody.style.display = "none";
};
cityhead[0].onclick = function() {
  cityhead[0].className = "province";
  cityhead[1].className = "";

  citybody[0].className = "active";
  citybody[1].className = "";
};
cityhead[1].onclick = function() {
  cityhead[1].className = "province";
  cityhead[0].className = "";
  citybody[1].className = "active";
  citybody[0].className = "";
};
// document.body.onclick=function (el) {
//     addresshead.classList.remove("addressheadchange");
//     addressbody.style.display = "none";
//   el.stopPropagation()
// }

// 二级菜单
var mainnav = document.querySelectorAll(".category-item");
var leftnav = document.querySelectorAll(".left-list-item");
var menu = document.querySelector(".menu");
var twoList = document.querySelector(".twomenu");
console.log(mainnav);
for (let i = 0; i < mainnav.length; i++) {
  mainnav[i].onmouseenter = function() {
    console.log('进入');
    for (let j = 0; j < mainnav.length; j++) {
      mainnav[j].classList.remove("active");
      leftnav[j].classList.remove("active");
    }

    mainnav[i].classList.add("active");
    leftnav[i].classList.add("active");
    menu.style.display = "flex";
  };
}
twoList.onmouseleave = function() {
  for (let j = 0; j < mainnav.length; j++) {
    mainnav[j].classList.remove("active");
    leftnav[j].classList.remove("active");
  }
  menu.style.display = "none";
};

//搜索框
var inputs = document.querySelector(".c-search-input");
var inputhelp = document.querySelector(".c-help");
inputs.onfocus = function(el) {
  inputhelp.classList.add("active");
  el.stopPropagation();
};
inputs.onblur = function() {
  inputhelp.classList.remove("active");
};

//轮播图
var imgs = document.querySelectorAll(".lunbotubai img");
var liss = document.querySelectorAll(".zhishideng ul li");
var btnleft = document.querySelector(".btnleft");
var btnright = document.querySelector(".btnright");
var box = document.querySelector(".lunbotubai");

var indexs = 0;
var lastindex = 0;

function nextimg() {
  indexs = indexs == imgs.length - 1 ? 0 : indexs + 1;
  showimg();
}
function previmg() {
  indexs = indexs == 0 ? imgs.length - 1 : indexs - 1;
  showimg();
}
function showimg() {
  imgs[lastindex].classList.remove("active");
  liss[lastindex].classList.remove("current");
  imgs[indexs].classList.add("active");
  liss[indexs].classList.add("current");
  lastindex = indexs;
}

for (var i = 0; i < liss.length; i++) {
  liss[i].indexs = i;
  liss[i].onmouseenter = function() {
    indexs = this.indexs;
    showimg();
  };
}

btnleft.onclick = function() {
  previmg();
};
btnright.onclick = function() {
  nextimg();
};
var timer = setInterval(nextimg, 3000);
box.onmouseenter = function() {
  clearInterval(timer);
};
box.onmouseleave = function() {
  timer = setInterval(nextimg, 3000);
};

// 侧边购物袋
var logins = document.querySelector(".my-icon .login");
var shop = document.querySelector(".shop-bar");
var login = document.querySelector(".shop-bar .login");
var fixedbar = document.querySelector(".fixedbar");
var closebar = document.querySelector(".login .upbox .close");
var myicon = document.querySelector(".side-cnt .my-icon");
var closebar2 = document.querySelector(".shop-bar .upbox .noopen");
//购物袋事件
console.log(closebar2);
shop.onclick = function() {
  login.style.transform = "translateX(-311px)";
};
fixedbar.onmouseleave = function() {
  login.style.transform = "translateX(311px)";
};
closebar2.onclick = function(e) {
  console.log('---------');
  login.style.transform = "translateX(311px)";
  e.stopPropagation()
};

//账号事件
myicon.onmouseenter = function() {
  logins.style.transform = "translateX(-311px)";
};
myicon.onmouseleave = function() {
  logins.style.transform = "translateX(311px)";
};
closebar.onclick = function() {
  logins.style.transform = "translateX(311px)";
};

//倒计时
var HOUR = document.querySelector(".J-sp-time-H");
var MINUTE = document.querySelector(".J-sp-time-M");
var SECOND = document.querySelector(".J-sp-time-S");
var hours = HOUR.innerHTML;
var minutes = MINUTE.innerHTML;
var seconds = SECOND.innerHTML;
var time = hours * 60 * 60 + minutes * 60 + seconds * 1;
var timer;
setInterval(updateTime, 1000);

function updateTime() {
  time--;
  if (time == 0) {
    clearInterval(timer);
    timer = null;
  }
  var hour = Math.floor(time / 3600);
  var minute = Math.floor((time % 3600) / 60);
  var second = time % 60;
  if (hour < 10) {
    hour = "0" + hour;
  }
  if (minute < 10) {
    minute = "0" + minute;
  }
  if (second < 10) {
    second = "0" + second;
  }
  HOUR.innerHTML = hour;
  MINUTE.innerHTML = minute;
  SECOND.innerHTML = second;
}
